<?php
//include("../../db/DBConnection.php");
//DataBase::createConection();
include("../../db/cart_db.php");
include("../../db/cart_functions.php");

if (isset($_REQUEST['command']) && isset($_REQUEST['pid'])) {
    if ($_REQUEST['command'] == 'delete' && $_REQUEST['pid'] > 0) {
        remove_product($_REQUEST['pid']);
    } else if ($_REQUEST['command'] == 'clear') {
        unset($_SESSION['cart']);
    } else if ($_REQUEST['command'] == 'update') {
        $max = count($_SESSION['cart']);
        for ($i = 0; $i < $max; $i++) {
            $pid = $_SESSION['cart'][$i]['productid'];
            $q = intval($_REQUEST['product' . $pid]);
            if ($q > 0 && $q <= 999) {
                $_SESSION['cart'][$i]['qty'] = $q;
            } else {
                $msg = 'Alguns produtos não foram atualizados - a quantidade deve ser uma número entre 1 e 999.';
            }
        }
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Bfat</title>
        <!-- <link rel="shortcut icon" href="../imagens/favicon.ico" /> -->
        <link href="../../viewer/css/styles.css" rel="stylesheet" type="text/css" />
        <link href="../../viewer/css/button.css" rel="stylesheet" type="text/css" />
        <script language="javascript">
            function del(pid){
                if(confirm('Você realmente deseja apagar este item?')){
                    document.form1.pid.value=pid;
                    document.form1.command.value='delete';
                    document.form1.submit();
                }
            }
            function clear_cart(){
                if(confirm('Isto irá esvaziar seu pedido! Deseja continuar?')){
                    document.form1.command.value='clear';
                    document.form1.submit();
                }
            }
            function update_cart(){
                document.form1.command.value='update';
                document.form1.submit();
            }


        </script>
        <style>
            div#side-col
            {
                top: 400px;
            }
            td{
                padding:15px;
                background-color:#FFFFFF;

            }
        </style>
    </head>

    <body>
        <div id="banner">
            <center><a href="../../viewer/vIndex.php"><img width="200" height="200" src="../../viewer/images/logo.png" /></a>
                <nav><ul>
                        <li><a href="../../viewer/shopping_cart/products.php">Cardápio</a></li>
                        <li><a href="../../viewer/vLocalizacao.php">Localização</a></li>
                        <li><a href="../../viewer/vFotos.php">Fotos</a></li>
                        <li><a href="../../viewer/vContato.php">Contato</a></li>
                        <li><a href="../../viewer/vLogin.php">Painel de controle</a></li>
                    </ul></nav></center>
        </div>

        <div id="side-col">
            <br><br>
                    <form name="form1" method="post">
                        <input type="hidden" name="pid" />
                        <input type="hidden" name="command" />
                        <div style="margin:0px auto; width:600px;" >
                            <div style="padding-bottom:10px">
                                <!--<h1 align="center">Your Shopping Cart</h1>-->
                                <input type="button" value="Continuar comprando" onclick="window.location='products.php'" />
                            </div>
                            <div style="color:#F00"><?php if (isset($msg)) echo $msg ?></div>
                            <table border="0" cellpadding="5px" cellspacing="1px" style="font-family:Verdana, Geneva, sans-serif; font-size:11px; background-color:#E1E1E1" width="100%">
                                <?php
                                if (isset($_SESSION['cart']) && is_array($_SESSION['cart'])) {
                                    echo '<tr bgcolor="#FFFFFF" style="font-weight:bold"><td>Serial</td><td>Nome</td><td>Preço</td><td>Qtd</td><td>Valor</td><td>Opções</td></tr>';
                                    $max = count($_SESSION['cart']);
                                    for ($i = 0; $i < $max; $i++) {
                                        $pid = $_SESSION['cart'][$i]['productid'];
                                        $q = $_SESSION['cart'][$i]['qty'];
                                        $pname = get_product_name($pid);
                                        if ($q == 0)
                                            continue;
                                        ?>
                                        <tr bgcolor="#FFFFFF"><td><?php echo $i + 1 ?></td><td><?php echo $pname ?></td>
                                            <td>R$ <?php echo get_price($pid) ?></td>
                                            <td><input type="text" name="product<?php echo $pid ?>" value="<?php echo $q ?>" maxlength="3" size="2" /></td>                    
                                            <td>R$ <?php echo get_price($pid) * $q ?></td>
                                            <td><a href="javascript:del(<?php echo $pid ?>)">Remover</a></td></tr>
                                        <?php
                                    }
                                    ?>
                                    <tr><td><b>Total: R$<?php echo get_order_total() ?></b></td><td colspan="5" align="right"><input type="button" value="Limpar" onclick="clear_cart()"><input type="button" value="Atualizar" onclick="update_cart()"><input type="button" value="Fechar pedido" onclick="window.location='billing.php'"></td></tr>
                                                        <?php
                                                    }
                                                    else {
                                                        echo "<tr bgColor='#FFFFFF'><td>Não há itens no seu pedido!</td>";
                                                    }
                                                    ?>
                                                    </table>
                                                    </div>
                                                    </form>
                                                    <br></br>
                                                    </div>
                                                    </body>
                                                    <footer>
                                                        <?php require("../../viewer/vLayoutFooter.php"); ?>
                                                    </footer>
                                                    </html>